<template>
  <div class="app-container hk-flex-column">
    <header class="hk-flex">
      <div class="hk-flex left">
        <img src="@/assets/images/tj-icon-04.png" alt />
        <div class="total-area">
          <p class="font-size16 total-title">全部人员</p>
          <span class="font-c-blue">{{ countObj.opUserCount || 0 }}</span>
        </div>
      </div>
      <div v-if="countObj.postCount.length" class="tab-list tab-bg hk-flex">
        <div class="icon hk-flex-column">
          <img :src="require(`@/assets/images/icon1@2x.png`)" alt />
          <p>运维岗位</p>
        </div>
        <div class="hk-flex-column flex-1">
          <div v-for="(i, idx) in countObj.postCount.slice(0, 3)" :key="idx" class="sp-flex">
            <span>{{ i.postName }}</span>
            <i>{{ i.postCount }}</i>
          </div>
        </div>
      </div>
      <div v-if="countObj.secLevelCount.length" class="tab-list tab-bg hk-flex">
        <div class="icon hk-flex-column">
          <img :src="require(`@/assets/images/icon2@2x.png`)" alt />
          <p>涉密等级</p>
        </div>
        <div class="hk-flex-column flex-1">
          <el-row>
            <el-col v-for="(i, idx) in countObj.secLevelCount.slice(0, 4)" :key="idx" :span="12" class="spm-flex">
              <span>{{ i.secLevel }}</span>
              <i>{{ i.levelCount }}</i>
            </el-col>
          </el-row>
        </div>
      </div>
    </header>
    <div class="hk-flex-column mt20 tab-content">
      <search-form
        :data="queryParams"
        :field-list="filterInfo.fieldList"
        :list-type-info="dictList"
        @handleSearch="handleQuery"
        @handleReset="resetQuery" />
      <el-row style="position: absolute; right: 20px; top: 50px">
        <el-button type="primary" size="mini" icon="el-icon-plus" @click="handleAdd">新增</el-button>
        <el-button size="small" @click="importTemplate">下载模板</el-button>
        <el-button size="small" @click="handleImport">导入</el-button>
        <el-button size="small" @click="handleExport">导出查询结果</el-button>
      </el-row>

      <hk-table
        :table-data="tableData"
        :table-columns="tableList"
        :selection="false"
        :show-index="true"
        :total="total"
        :loading="loading"
        :table-height="80"
        operate
        :operate-width="220"
        :query-params="queryParams"
        @handleSelectionChange="handleSelectionChange"
        @changePage="getDataList">
        <!-- 操作权限 -->
        <template #operate="scope">
          <el-button size="mini" type="text" @click="handleDetail(scope.row)">查看</el-button>
          <el-button size="mini" type="text" @click="handleEdit(scope.row)">编辑</el-button>
          <el-button size="mini" type="text" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </hk-table>
      <hk-upload
        ref="import_dia"
        :url="url"
        :title="title"
        :accept="accept"
        @submit="
          getDataList()
          getCount()
        " />
    </div>
  </div>
</template>

<script>
import { pageList, count, listByType, deleteItem } from '@/api/specialProject/personnel'
import SearchForm from '@/components/HkSearchform/index.vue'
import HkTable from '@/components/Hk-Table/index.vue'
export default {
  components: {
    SearchForm,
    HkTable,
  },
  beforeRouteLeave(to, from, next) {
    console.log(to, '路由')
    if (to.path === '/specialProject/personnelDetail') {
      const condition = JSON.stringify(this.queryParams)
      sessionStorage.setItem('condition', condition)
    } else {
      sessionStorage.removeItem('condition')
    }
    next()
  },
  beforeRouteEnter(to, from, next) {
    console.log(to, '路由')
    if (from.path !== '/specialProject/personnelDetail') {
      sessionStorage.removeItem('condition')
    }
    next()
  },
  data() {
    return {
      queryParams: {
        current: 1,
        size: 10,
      },
      checkedList: [],
      tableData: [],
      total: 0,
      countObj: {
        postCount: [],
        secLevelCount: [],
      },
      url: window.baseUrl.action + '/user/import',
      title: '导入运维人员',
      accept: '.xlsx',
      loading: false,
      tableList: [
        { label: '运维人员名称', param: 'userName' },
        { label: '状态', param: 'status', width: 80 },
        { label: '运维岗位', param: 'userPostName' },
        { label: '关联应用', param: 'userPostProduct' },
        { label: '受雇单位', param: 'unitName' },
        { label: '归口管理部门', param: 'deptName' },
        { label: '离部时间', param: 'departureDate' },
        { label: '涉密等级', param: 'secLevel', width: 80 },
      ],
      filterInfo: {
        fieldList: [
          { label: '关键字', type: 'input', field: 'keyword', placeholder: '请输入运维人员名称', maxlength: 20 },
          { label: '涉密等级', type: 'select', field: 'level', list: 'secLevelList' },
        ],
      },
      dictList: {
        secLevelList: [],
      },
    }
  },
  created() {
    var cd = sessionStorage.getItem('condition')
    if (cd) {
      var condition = JSON.parse(cd)
      this.queryParams = condition
    }
    if (this.$route.params.key) {
      this.queryParams[this.$route.params.key] = this.$route.params.value
    }
  },
  mounted() {
    this.getTypes()
    this.getDataList()
    this.getCount()
  },
  methods: {
    beforeunloadHandler(e) {
      alert(1111)
      console.log(e, 'eeeee')
    },
    //  查询
    handleQuery() {
      this.queryParams.current = 1
      this.getDataList()
    },
    //  重置
    resetQuery() {
      this.queryParams.current = 1
      this.queryParams.size = 10
      this.getDataList()
    },
    getDataList(val) {
      this.queryParams = { ...this.queryParams, ...val }
      this.loading = true
      pageList(this.queryParams).then((res) => {
        this.loading = false
        this.tableData = res.list
        this.total = res.total
        const condition = JSON.stringify(this.queryParams)
        sessionStorage.setItem('condition', condition)
      })
    },
    getTypes() {
      listByType('secLevel').then((res) => {
        this.dictList.secLevelList = res.map((i) => {
          return {
            label: i.dictValue,
            value: i.dictKey,
          }
        })
      })
    },
    getCount() {
      count().then((res) => {
        this.countObj = res
        this.countObj.postCount = res.postCount.sort((a, b) => {
          return b.postCount - a.postCount
        })
        // this.countObj.secLevelCount = res.secLevelCount
        this.countObj.secLevelCount = res.secLevelCount.sort((a, b) => {
          return b.levelCount - a.levelCount
        })
      })
    },

    handleSelectionChange(obj) {
      this.checkedList = obj
    },
    handleDetail(row) {
      this.$router.push(window.prefix + '/specialProject/personnelDetail?showDetail=true&id=' + row.id)
    },
    handleAdd() {
      this.$router.push(window.prefix + '/specialProject/personnelDetail')
    },
    handleEdit(row) {
      this.$router.push(window.prefix + '/specialProject/personnelDetail?id=' + row.id)
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const id = row.id || this.ids.join(',')
      this.$modal
        .confirm('是否确认删除所选数据项？')
        .then(function () {
          return deleteItem(id)
        })
        .then(() => {
          this.$modal.msgSuccess('删除成功')
          this.getDataList()
          this.getCount()
        })
        .catch((error) => {
          console.log(error)
        })
    },
    /** 导入按钮操作 */
    handleImport() {
      this.$refs.import_dia.show()
    },
    /** 下载模板操作 */
    importTemplate() {
      this.gDownload(window.baseUrl.action + '/user/template', {}, `运维人员导入模板.xlsx`)
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        window.baseUrl.action + '/user/export',
        {
          ...this.queryParams,
        },
        `运维人员${new Date().getTime()}.xlsx`,
        'daochu'
      )
    },
  },
}
</script>

<style lang="scss" scoped>
#app.dark-theme {
  .app-container,
   header,
  .tab-content {
    background: transparent !important;
    color: #fff;
  }
  .tab-bg {
    background: rgba(59, 139, 255, 0.1) !important;
  }
  .spm-flex {
    i {
      color: #4b6eef !important;
    }
  }
  .sp-flex i {
    color: #4b6eef !important;
  }
}
img {
  width: 60px;
}
.app-container {
  background: #eef0f5 !important;
  padding: 0;
  header {
    padding: 20px;
    background: #fff;
    height: 160px;
    border-radius: 4px;

    .left {
      width: 20%;
      max-width: 250px;
      img {
        width: 100px;
      }
    }
    .total-area {
      margin-left: 5px;
    }
    .tab-bg {
      background: #f3f5fb;
      padding: 20px;
      border-radius: 4px;
    }
    .icon {
      align-items: center;
      margin-right: 20px;
      p {
        margin: 0;
        margin-top: 10px;
      }
    }
    .spm-flex {
      height: 100%;
      padding-right: 10%;
      display: flex;
      align-items: center;
      justify-content: space-between;
      i {
        font-style: normal;
        font-size: 18px;
        color: #333333;
        // line-height: 24px;
        font-weight: 600;
      }
    }
    .sp-flex {
      span {
        max-width: 90%;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
      }
      i {
        font-style: normal;
        font-size: 18px;
        color: #333333;
        line-height: 24px;
        font-weight: 600;
      }
    }
    .tab-list {
      height: 100%;
      margin-right: 20px;
      flex: 1;
      max-width: 360px;
    }
  }
}
.tab-content {
  flex: 1;
  overflow: auto;
  padding: 20px;
  position: relative;
  background: #fff;
}

.total-title {
  margin-bottom: 15px;
  margin-top: 6px;
  line-height: 24px;
}
.font-size16 {
  font-size: 16px;
}
.flex-1 {
  width: calc(100% - 80px);
  justify-content: space-between;
  height: 100%;
}
.font-c-blue {
  color: #4b6eef;
  font-size: 36px;
}
.mt-20 {
  margin-top: 20px;
}
::v-deep {
  .content {
    padding-bottom: 0;
  }
}
</style>
